<?php

$_this =& $this;

/* @var $_this View */
/* @var $form FormHelper */
/* @var $editForm EditFormHelper */
/* @var $html HtmlHelper */
/* @var $ehtml EHtmlHelper */
/* @var $javascript JavascriptHelper */
/* @var $jqax JqaxHelper */
/* @var $filterBlock FilterBlock */

switch ($mode) {

case "select_client":
    if (count($list->rows)) {

    ?><?=$this->renderElement('pagination', array('paging' => $list->pagination));?><?php

    ?><table class="table" cellpadding="0"><?php
    ?><thead><?php
        ?><tr><?php
            ?><th class="nw"><?php
                ?><?=$this->renderElement('sort_field', array('obj' => $list->order, 'field' => 'name', 'domain' => $po_domain));?><?php
            ?></th><?php
            ?><th class="nw"><?php
                ?><?=$this->renderElement('sort_field', array('obj' => $list->order, 'field' => 'contract', 'domain' => $po_domain));?><?php
            ?></th><?php
            ?><th class="nw"><?php
                ?><?=$this->renderElement('sort_field', array('obj' => $list->order, 'field' => 'reg_code', 'domain' => $po_domain));?><?php
            ?></th><?php
            ?><th class="nw"><?php
                ?><?=$this->renderElement('sort_field', array('obj' => $list->order, 'field' => 'tools', 'domain' => $po_domain));?><?php
            ?></th><?php
            ?><th class="nw"><?php
                ?><?=$this->renderElement('sort_field', array('obj' => $list->order, 'field' => 'balance', 'msg' => 'balance/free', 'domain' => $po_domain));?><?php
            ?></th><?php
            ?><th class="actions nw"></th><?php
        ?></tr><?php
    ?></thead><?php
    ?><tbody><?php

        $i = 0;
        foreach ($list->rows as $item) {
            $class = '';
        	if ($i++ % 2 != 0) {
        		$sep = strlen($class) ? ' ' : '';
        		$class .= $sep.'altrow';
        	}
        	$class = strlen($class) ? ' class="'.$class.'"' : '';

        	$create_report_url = array('controller' => 'reports', 'action' => 'consolidate_create', 'client_id' => $item[$modelName][$PKName]);

            ?><tr <?=$class;?>><?php
        		?><td class="nw"><?php
                    ?><?=$html->link($item[$modelName]['name'], $create_report_url);?><?php
                ?></td><?php
        		?><td class="nw"><?php
                    if (!empty($item[$modelName]['contract'])) {
                        $start_date_caption = __d($po_domain, 'contract_start_date', true);
                        $start_finish_caption = __d($po_domain, 'contract_finish_date', true);
                        $start_date = !empty($item[$modelName]['contract_start_date']) ? Date::Short(Date::SQL2UNIX($item[$modelName]['contract_start_date']), false) : '—';
                        $finish_date = !empty($item[$modelName]['contract_finish_date']) ? Date::Short(Date::SQL2UNIX($item[$modelName]['contract_finish_date']), false) : '—';
                        $hint = $start_date_caption.': '.$start_date;
                        $hint .= "&#13;";
                        $hint .= $start_finish_caption.': '.$finish_date;
                        ?><span title="<?=($hint);?>" class="hinteble"><?php
                            ?><?=($item[$modelName]['contract']);?><?php
                        ?></span><?php
                    } else {
                        ?>—<?php
                    }
                ?></td><?php
                ?><td class="nw"><?php
                    ?><?=($item[$modelName]['reg_code']);?><?php
                ?></td><?php
                ?><td class=""><?php
                    if (!empty($item[$modelName]['tools'])) {
                        $str = array();
                        foreach ($tools_list as $k => $v) {
                            if (in_array($k, $item[$modelName]['tools'])) {
                                $str[] = '<span class="nw">'.h($v).'</span>';
                            }
                        }
                        ?><?=implode(', ', $str);?><?php
                    } else {
                        ?>—<?php
                    }
                ?></td><?php
                ?><td class="nw"><?php
                    ?><div class="align-r"><?php
                    ?><?=$this->renderElement('sum', array('sum'=>$item[$modelName]['balance']));?><?php
                    ?></div><?php
                ?></td><?php
                ?><td class="actions"><?php
                    ?><table class="sys_clear" cellpadding="0"><?php
                    ?><tr class="sys_clear"><?php
                        ?><?=$ehtml->iconLink("report_admining", __d("candidates", 'do_report', true), $create_report_url);?><?php
                    ?></tr><?php
                    ?></table><?php
                ?></td><?php
        	?></tr><?php
        }

    ?></tbody><?php
    ?></table><?php

    ?><?=$this->renderElement('pagination', array('paging' => $list->pagination));?><?php

    } else {
        ?><span class="additional_msg"><?php
            ?><?=__d($po_domain, 'List is empty', true);?><?php
        ?></span><?php
    }
    break;

case "normal":
    Page::addJS('cons_report');

    $__tool_form_id = 'select_report_form';
    $__submit_button_id = 'submit_report';
    $__select_tool_control_name = "data[{$modelName}][test_id]";
    $__candidates_control_name = "data[{$modelName}][testing_id]";
    $__candidates_table_id = "ConsReportCreateCandidateTable";

    foreach ($tests_list as &$test) {
        foreach ($test['Test']['Reports'] as &$report) {
            $report['html_price'] = $editForm->drawAmount($report['price'], 'EP');
            $report['txt_price'] = $editForm->drawAmountTXT($report['price'], 'EP');
        }
    }

    $action_url_ar = array('controller' => 'reports', 'action' => 'consolidate_create');
    if (Auth::o()->is('admin')) {
        $action_url_ar["client_id"] = $client_id;
    }
    ?><?=$editForm->create($modelName, array('id' => $__tool_form_id, 'url' => $action_url_ar));?><?php

    vendor('json');

    Page::appendJSCode((
    '__ToolGUIEnv = new ToolGUI('.
    json::drawToComment(array(
        'client_id'                     => $client_id,
        'url_base'                      => Application::rootUrlPath(),
        'candidates_control_name'       => $__candidates_control_name,
        'candidates_table_id'           => $__candidates_table_id,
        'select_report_control_name'    => "data[{$modelName}][report_id]",
        'select_norm_control_name'      => "data[{$modelName}][norm_id]",
        'selected_candidates_table_id'  => 'selectedCandidatesTable',
        'submit_button_id'              => $__submit_button_id,
        'tool_form_id'                  => $__tool_form_id,
        'select_tool_control_name'      => $__select_tool_control_name,
        'reports_title'                 => __d($po_domain, 'Reports', true),
        'norms_title'                   => __d($po_domain, 'Norms', true),
        'candidates_title'              => __d('testings', 'completed_testings_title', true),
        'selected_candidates_title'     => __d($po_domain, 'Selected candidates', true),
        'po'   => array(
            'cancel'   => __d(null, 'cancel', true),
            'selected_candidate_absent'   => __d($po_domain, 'Selected candidates is absent', true),
            'confirm_begin' => __d('candidates', 'BEGIN_report_create_confirm'.(Auth::o()->is('admin') ? '_for_admin' : ''), true),
            'confirm_end' => __d('candidates', 'END_report_create_confirm', true),
            'confirm_middle_1' => __d('candidates', 'CONS_1_report_create_confirm', true),
            'confirm_middle_2' => __d('candidates', 'CONS_2_report_create_confirm', true),
            'confirm_middle_3' => __d('candidates', 'CONS_3_report_create_confirm', true),
            'currencyTXT' => $editForm->drawCurrencyTXT('EP'),
            'candidate_plurar_0' => __d('candidates', 'candidate_plurar_0', true),
            'candidate_plurar_1' => __d('candidates', 'candidate_plurar_1', true),
            'candidate_plurar_2' => __d('candidates', 'candidate_plurar_2', true),
            'candidate_plurar_3' => __d('candidates', 'candidate_plurar_3', true),
        ),
        'selected_candidates_headers'   => array(
            "surname" => __d('candidates', 'surname', true),
            "name" => __d('candidates', 'name', true),
            "group" => __d('candidates', 'group', true),
            "test_name" => __d('testings', 'test_name', true),
            "pass_date" => __d('testings', 'pass_date', true),
        ),
        'reports_headers'               => array(
            "name" => __d('reports', 'name', true),
            "price" => __d('reports', 'price', true),
        ),
        'norms_headers'               => array(
            "name" => __d('reports', 'name', true),
        ),
    )).
    ', '.
    json::drawToComment($tests_list).
    ');'
    ));?><?php

    	ob_start();

    if (count($tests_list)) {

    ?><table class="table" cellpadding="0"><?php
    ?><thead><?php
        ?><tr><?php
            ?><th class="nw"><?php
                ?><?=__d('tests', 'name', true);?><?php
            ?></th><?php
            ?><th class="nw"></th><?php
        ?></tr><?php
    ?></thead><?php
    ?><tbody><?php

        $i = 0;
        foreach ($tests_list as $item) {

            $class = '';
        	if ($i++ % 2 != 0) {
        		$sep = strlen($class) ? ' ' : '';
        		$class .= $sep.'altrow';
        	}
        	$class = strlen($class) ? ' class="'.$class.'"' : '';

            ?><tr <?=$class;?>><?php
        		?><td class="nw"><?php
                    ?><?=$item['Test']['name'];?><?php
                ?></td><?php
                ?><td class="nw"><?php
                    ?><input type="radio" name="<?=$__select_tool_control_name;?>" value="<?=$item['Test']['id'];?>" /><?php
                ?></td><?php
        	?></tr><?php
        }

    ?></tbody><?php
    ?></table><?php

    } else {
        ?><span class="additional_msg"><?php
            ?><?=__d($po_domain, 'tests_List is empty', true);?><?php
        ?></span><?php
    }

    	$content = ob_get_clean();

        ?><?=$editForm->header(__d($po_domain, 'Tests', true));?><?php
        ?><?=$editForm->cell($content, array('class' => 'non-top-margin'));?><?php

    	?><?=$editForm->startGroup($__candidates_table_id);?><?php
            ?><?=$editForm->cell('');?><?php
        ?><?=$editForm->endGroup();?><?php

    	?><?=$editForm->startGroup('reports_section');?><?php
            ?><?=$editForm->cell('');?><?php
        ?><?=$editForm->endGroup();?><?php

    	?><?=$editForm->startGroup('norms_section');?><?php
            ?><?=$editForm->cell('');?><?php
        ?><?=$editForm->endGroup();?><?php


    	$buttonsHTML = '';
    	$buttonsHTML .= $form->hidden('testing_id');

    	$buttonsHTML .= (strlen($buttonsHTML) ? ' ' : '').$editForm->back("/{$url_part}/");
    	$buttonsHTML .= (strlen($buttonsHTML) ? ' ' : '').$editForm->submit(__d($po_domain, 'create_report', true), array('id'=>$__submit_button_id));
    	if (strlen($buttonsHTML)) {
        	?><?=$editForm->cell($buttonsHTML);?><?php
    	}

    ?><?=$editForm->end();?><?php

    break;
}

?>
